<?php
/**
 * Build the list of contacts according to the area
 * 
 */

use Fox\Dao\SqlFragment;
use Hive\Ressource;
$xview = Xview::instance();
$area = $xview->val('area');
$cpid = $xview->val('cpid');
if(!empty($cpid)) {
	$query = Fox\Dao::infos(Ressource::className())->handle()->createQueryBuilder();
	
	$query->select('c.ressource_id','c.last_name','c.first_name','c.email','exp.expid','exp.compid','exp.jobtitle','exp.importance')
			->from('ressources','c')
			->innerJoin('c','ressource_experiences','exp','c.ressource_id = exp.ressource_id');
	
	$fragments[] = new SqlFragment('c.loekz = ? AND exp.compid = ? ',array(0,$cpid));
	switch ($area) {
		case 'vip' :
			$fragments[] = new SqlFragment('exp.current = ? AND exp.importance > 0',array(1));
			break;
		case 'all' :
			$fragments[] = new SqlFragment('exp.current = ?',array(1));
			break;
		case 'old' :
			$fragments[] = new SqlFragment('exp.current = ? OR exp.current IS NULL',array(0));
			break;
	}
	
	$fragment = SqlFragment::combine($fragments);
	$result = $fragment->pushIn($query)->execute()->fetchAll(PDO::FETCH_ASSOC);
} else {
	$result = array();
}

$grid = $xview->component('company','contacts');
$grid->initialize($result);
return $grid->display();